package org.jahia.sqlprofiler.gui;

import javax.swing.event.EventListenerList;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Jahia Ltd</p>
 * @author not attributable
 * @version 1.0
 */

public abstract class AbstractChartModel implements ChartModel {

    protected EventListenerList listenerList = new EventListenerList();
    ChartModelEvent chartModelEvent = null;

    public void addChartModelListener(ChartModelListener l) {
        listenerList.add(ChartModelListener.class, l);
    }

    public void removeChartModelListener(ChartModelListener l) {
        listenerList.remove(ChartModelListener.class, l);
    }

    // Notify all listeners that have registered interest for
    // notification on this event type.  The event instance
    // is lazily created using the parameters passed into
    // the fire method.

    protected void fireChartDataChanged() {

        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == ChartModelListener.class) {
                // Lazily create the event:
                if (chartModelEvent == null)
                    chartModelEvent = new ChartModelEvent(this);
                ( (ChartModelListener) listeners[i + 1]).chartDataChanged(chartModelEvent);
            }
        }


    }
}